from django.db import models
from django.contrib.auth import get_user_model


class SystemLog(models.Model):
    """系统日志模型"""
    user = models.ForeignKey(
        'users.User',
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        verbose_name='操作用户'
    )
    action = models.CharField(max_length=50, verbose_name='操作类型')
    target_type = models.CharField(max_length=50, blank=True, null=True, verbose_name='操作对象类型')
    target_id = models.IntegerField(blank=True, null=True, verbose_name='操作对象ID')
    description = models.TextField(blank=True, null=True, verbose_name='操作描述')
    ip_address = models.GenericIPAddressField(blank=True, null=True, verbose_name='IP地址')
    user_agent = models.TextField(blank=True, null=True, verbose_name='用户代理信息')
    request_data = models.JSONField(blank=True, null=True, verbose_name='请求数据')
    response_status = models.IntegerField(blank=True, null=True, verbose_name='响应状态码')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='操作时间')

    class Meta:
        db_table = 'system_logs'
        verbose_name = '系统日志'
        verbose_name_plural = '系统日志'
        ordering = ['-created_at']

    def __str__(self):
        return f"{self.action} - {self.created_at}"